/*
 * 文件名：BpastEffectCollectServiceImpl.java
 * 版权：Copyright by www.infopub.top
 * 描述：
 * 修改人：wuzhiyang
 * 修改时间：2020年3月18日
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */

package top.infopub.job.bpast.service.impl;


import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import top.infopub.job.bpa.service.EffectPlanService;
import top.infopub.job.bpa.service.EffectSureService;
import top.infopub.job.bpa.service.OutcaeSureService;
import top.infopub.job.bpa.service.ProContractService;
import top.infopub.job.bpast.service.BpastEffectCollectService;
import top.infopub.job.bpast.service.EffectCollectService;
import top.infopub.job.hr.service.OrgInfoService;
import top.infopub.job.util.ParserUtil;
import top.infopub.model.bpa.ProContract;
import top.infopub.model.bpast.EffectCollect;
import top.infopub.model.hr.OrgInfo;
import top.infopub.tdp.util.BizParaUtil;

import com.google.common.collect.Maps;


@Service
public class BpastEffectCollectServiceImpl implements BpastEffectCollectService {

    @Autowired
    private OrgInfoService orgInfoService;

    @Autowired
    private OutcaeSureService outcaeSureService;

    @Autowired
    private ProContractService proContractService;

    @Autowired
    private EffectCollectService effectCollectService;

    @Autowired
    private EffectSureService effectSureService;

    @Autowired
    private EffectPlanService effectPlanService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void bpastEffectCollectJob() {
        //查询所有项目合同名称
        Map<String, Object> proContractWhere = Maps.newHashMap();
        proContractWhere.put("processStatus", "9");
        List<ProContract> proContracts = proContractService.selectByMapWhere(proContractWhere);
        for (ProContract proContract : proContracts) {
            EffectCollect effectCollect = new EffectCollect();
            //往表里存放项目信息
            effectCollect.setProCode(proContract.getProCode());
            effectCollect.setProName(proContract.getProName());
            //往表里存放公司信息
            OrgInfo orgInfo = new OrgInfo();
            orgInfo.setProjectCode(proContract.getProCode());
            orgInfo.setState(1);
            List<OrgInfo> orgInfos = orgInfoService.selectByEntityWhere(orgInfo);
            if (orgInfos.size() > 0) {
                // 注意：不归集南充项目
                if (StringUtils.endsWithIgnoreCase(orgInfos.get(0).getId(),
                    BizParaUtil.get("land_org_id"))) {
                    continue;
                }
                effectCollect.setOrgProName(orgInfos.get(0).getCompName());
                effectCollect.setHrOrgProId(orgInfos.get(0).getId());
                OrgInfo parentOrgInfo = new OrgInfo();
                parentOrgInfo.setId(orgInfos.get(0).getParentId());
                parentOrgInfo.setState(1);
                List<OrgInfo> parentOInfos = orgInfoService.selectByEntityWhere(parentOrgInfo);
                if (parentOInfos.size() > 0) {
                    effectCollect.setHrOrgAreaName(parentOInfos.get(0).getCompName());
                    effectCollect.setHrOrgAreaId(parentOInfos.get(0).getId());
                }
            }
            //存放时间信息
            effectCollect.setBelongsYear(ParserUtil.getYearOfPrevMonth());
            effectCollect.setBelongsMonth(ParserUtil.getMonthOfPrevMonth());
            effectCollect.setStsMonth(ParserUtil.prevMonth());//上个月
            //计划创效金额(元)
            Map<String, Object> effectPlanWhere = Maps.newHashMap();
            effectPlanWhere.put("belongsYear", effectCollect.getBelongsYear());
            effectPlanWhere.put("months", effectCollect.getBelongsMonth());
            effectPlanWhere.put("proCode", effectCollect.getProCode());
            BigDecimal monthPlanAmount = effectPlanService.selectMonthPlanAmount(effectPlanWhere);
            effectCollect.setPlanEffectAmount(monthPlanAmount);
            //实际创效金额(元)
            Map<String, Object> effectFeeWhere = Maps.newHashMap();
            effectFeeWhere.put("stsMonth", effectCollect.getStsMonth());
            effectFeeWhere.put("hrOrgProId", effectCollect.getHrOrgProId());
            BigDecimal effectFee = effectSureService.selectTypeOfEffectFee(effectFeeWhere);
            effectCollect.setActEffectAmount(effectFee);
            //设计优化(元)
            /*Map<String, Object> effectFeeWhere1 = Maps.newHashMap();
            effectFeeWhere1.put("stsMonth", effectCollect.getStsMonth());
            effectFeeWhere1.put("hrOrgProId", effectCollect.getHrOrgProId());
            effectFeeWhere1.put("effectType", 1);
            BigDecimal effectFeeAmount1 = effectSureService.selectEffectFees(effectFeeWhere1);
            effectCollect.setDesignAmount(effectFeeAmount1);*/
            //前期费用优化(元)
            /*Map<String, Object> effectFeeWhere2 = Maps.newHashMap();
            effectFeeWhere2.put("stsMonth", effectCollect.getStsMonth());
            effectFeeWhere2.put("hrOrgProId", effectCollect.getHrOrgProId());
            effectFeeWhere2.put("effectType", 2);
            BigDecimal effectFeeAmount2 = effectSureService.selectEffectFees(effectFeeWhere2);
            effectCollect.setUpfrontAmount(effectFeeAmount2);*/
            //认质认价(元)
            /*Map<String, Object> effectFeeWhere3 = Maps.newHashMap();
            effectFeeWhere3.put("stsMonth", effectCollect.getStsMonth());
            effectFeeWhere3.put("hrOrgProId", effectCollect.getHrOrgProId());
            effectFeeWhere3.put("effectType", 3);
            BigDecimal effectFeeAmount3 = effectSureService.selectEffectFees(effectFeeWhere3);
            effectCollect.setQoaAmount(effectFeeAmount3);*/
            //人材机调差(元)
            /*Map<String, Object> effectFeeWhere4 = Maps.newHashMap();
            effectFeeWhere4.put("stsMonth", effectCollect.getStsMonth());
            effectFeeWhere4.put("hrOrgProId", effectCollect.getHrOrgProId());
            effectFeeWhere4.put("effectType", 4);
            BigDecimal effectFeeAmount4 = effectSureService.selectEffectFees(effectFeeWhere4);
            effectCollect.setAdjustAmount(effectFeeAmount4);*/
            //签证变更(元)
            /*Map<String, Object> effectFeeWhere5 = Maps.newHashMap();
            effectFeeWhere5.put("stsMonth", effectCollect.getStsMonth());
            effectFeeWhere5.put("hrOrgProId", effectCollect.getHrOrgProId());
            effectFeeWhere5.put("effectType", 5);
            BigDecimal effectFeeAmount5 = effectSureService.selectEffectFees(effectFeeWhere5);
            effectCollect.setVisaAmount(effectFeeAmount5);*/
            //招标差价(元)
            /*Map<String, Object> effectFeeWhere6 = Maps.newHashMap();
            effectFeeWhere6.put("stsMonth", effectCollect.getStsMonth());
            effectFeeWhere6.put("hrOrgProId", effectCollect.getHrOrgProId());
            effectFeeWhere6.put("effectType", 6);
            BigDecimal effectFeeAmount6 = effectSureService.selectEffectFees(effectFeeWhere6);
            effectCollect.setBidAmount(effectFeeAmount6);*/
            //小计  = 设计优化(元)+前期费用优化(元)+认质认价(元)+人材机调差(元)+签证变更(元)+招标差价(元)
            /*BigDecimal subTotalAmount = effectCollect.getDesignAmount().add(
                effectCollect.getUpfrontAmount()).add(effectCollect.getQoaAmount()).add(
                effectCollect.getAdjustAmount()).add(effectCollect.getVisaAmount()).add(
                effectCollect.getBidAmount());
            effectCollect.setSubTotalAmount(subTotalAmount);*/
            //其他二三次经营(元)
            /*Map<String, Object> effectFeeWhere9 = Maps.newHashMap();
            effectFeeWhere9.put("stsMonth", effectCollect.getStsMonth());
            effectFeeWhere9.put("hrOrgProId", effectCollect.getHrOrgProId());
            effectFeeWhere9.put("effectType", 9);
            BigDecimal effectFeeAmount9 = effectSureService.selectEffectFees(effectFeeWhere9);
            effectCollect.setTwoThreeProfit(effectFeeAmount9);*/
            //开源创效（元）
            Map<String, Object> effectFeeWhere20 = Maps.newHashMap();
            effectFeeWhere20.put("stsMonth", effectCollect.getStsMonth());
            effectFeeWhere20.put("hrOrgProId", effectCollect.getHrOrgProId());
            effectFeeWhere20.put("effectType", 20);
            BigDecimal effectFeeAmount20 = effectSureService.selectByEffectType(effectFeeWhere20);
            effectCollect.setOpenHeadAmount(effectFeeAmount20);
            //节流降本（元）
            Map<String, Object> effectFeeWhere30 = Maps.newHashMap();
            effectFeeWhere30.put("stsMonth", effectCollect.getStsMonth());
            effectFeeWhere30.put("hrOrgProId", effectCollect.getHrOrgProId());
            effectFeeWhere30.put("effectType", 30);
            BigDecimal effectFeeAmount30 = effectSureService.selectByEffectType(effectFeeWhere30);
            effectCollect.setCostReduceAmount(effectFeeAmount30);
            //数据状态(默认为有效)
            effectCollect.setDataFlag("1");
            //添加人 添加时间 最后修改人 最后修改时间
            effectCollect.setAddBy("system");
            effectCollect.setAddTime(new Date());
            effectCollect.setUptBy("system");
            effectCollect.setUptTime(new Date());
            effectCollectService.insert(effectCollect);
        }
    }
}
